package com.shioirikukuri.service.impl;

import com.shioirikukuri.entity.Food;
import com.shioirikukuri.mapper.FoodMapper;
import com.shioirikukuri.service.IFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Service
public class FoodServiceImpl implements IFoodService {

	@Autowired
	private FoodMapper foodMapper; // 注入FoodMapper用于数据库操作

	@Override
	public List<Food> getAllFoods() {
		return foodMapper.selectAllFoods();
	}

	@Override
	public Optional<Food> getFoodById(Long id) {
		return Optional.ofNullable(foodMapper.selectFoodById(id));
	}

	@Override
	public Food addFood(Food food) {
		foodMapper.insertFood(food);
		return food;
	}

	@Override
	public Food updateFood(Long id, Food food) {
		food.setId(id);
		foodMapper.updateFood(food);
		return food;
	}

	@Override
	public void deleteFood(Long id) {
		foodMapper.deleteFoodById(id);
	}


	@Override
	public List<Food> getFoodsByIds(Collection<Long> ids) {
		return foodMapper.selectByIds(ids);
	}



	/**
	 * 根据分类ID获取食物
	 * @param categoryId
	 * @return List<Food>
	 */
	@Override
	public List<Food> getFoodsByCategoryId(Long categoryId) {
		return foodMapper.selectFoodsByCategoryId(categoryId);
	}

	/**
	 * 更新食物图片
	 * @param id
	 * @param imageUrl
	 */
	 @Override
	public void updateImageUrlById(Long id, String imageUrl) {
		foodMapper.saveImageUrl(id, imageUrl);
		Food food = foodMapper.selectFoodById(id);
		food.setImageUrl(imageUrl);
		foodMapper.updateFood(food);
	 }



}
